package com.szewec.ps.ui.road.facilities.investigation.task.roadside.facilities;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.szewec.ps.R;
import com.szewec.ps.base.Constants;
import com.szewec.ps.base.Enums;
import com.szewec.ps.databinding.FragmentRoadFacilitiesBusStopBinding;
import com.szewec.ps.model.road.facilities.investigation.road.side.facilities.RoadFacilitiesBusStop;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTask;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTaskDTO;
import com.szewec.ps.model.selectmappoint.SelectedPoint;
import com.szewec.ps.myview.toast.bamtoast.BamToast;
import com.szewec.ps.ui.road.facilities.intersection.task.utils.RollUtils;
import com.szewec.ps.ui.road.facilities.intersection.task.views.RadioBoxView;
import com.szewec.ps.util.CommonUtils;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.TextViewUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 公交站点
 */
public class RoadFacilitiesBusStopFragment extends Fragment {
    private FragmentRoadFacilitiesBusStopBinding binding;

    // 路段设施调查任务DTO
    private RoadFacilitiesInvestigationTaskDTO.RoadFacilitiesTaskDTO roadFacilitiesTaskDTO;
    private RoadFacilitiesBusStop roadFacilitiesBusStop;

    // 接驳设施 数据字典
    private List<String> connectionFacilitiesDict = new ArrayList<>();
    // 多选框选中的数据
    private List<String> connectionFacilitiesList = new ArrayList<>();
    // 坐标
    private String selectedPoint;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_road_facilities_bus_stop, container, false);
        // 2、获取到视图
        View inflate = binding.getRoot();
        ButterKnife.bind(this, inflate);

        initData();
        initView();
        return inflate;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        // 通过Activity中的public方法来获取值
        roadFacilitiesTaskDTO = ((RoadsideFacilitiesActivity) context).pushFragmentData();
    }

    private void initData() {
        connectionFacilitiesDict = Enums.BusConnectionFacilitiesEnum.getEnumValueList();
        roadFacilitiesBusStop = roadFacilitiesTaskDTO.getRoadFacilitiesBusStop() == null ? new RoadFacilitiesBusStop() : roadFacilitiesTaskDTO.getRoadFacilitiesBusStop();
    }

    private void initView() {
        // 站台类型
        Integer platformForm = roadFacilitiesBusStop.getPlatformForm();
        // 线路模式
        Integer platformType = roadFacilitiesBusStop.getPlatformType();
        // 站台长度	默认值为“30.00m”
        BigDecimal platformLength = roadFacilitiesBusStop.getPlatformLength() == null ? new BigDecimal("30.00") : roadFacilitiesBusStop.getPlatformLength();
        // 到邻近路口距离	默认值为“50.55m”
        BigDecimal distanceNearIntersection = roadFacilitiesBusStop.getDistanceNearIntersection() == null
                ? new BigDecimal("50.55") : roadFacilitiesBusStop.getDistanceNearIntersection();
        // 人行道宽度	默认值为“2.00m”
        BigDecimal sidewalkWidth = roadFacilitiesBusStop.getSidewalkWidth() == null ? new BigDecimal("30.00") : roadFacilitiesBusStop.getSidewalkWidth();

        // 获取坐标
        SelectedPoint selectedPoint = JSONObject.parseObject(roadFacilitiesBusStop.getStationCoordinates(), SelectedPoint.class);
        selectedPoint = selectedPoint == null ? new SelectedPoint() : selectedPoint;
        // 数据库存放的是1、直线式，2港湾式
        platformType = platformType == null ? RadioBoxView.ONE : platformType;
        // 数据库存放的是1、正式站，2简易站
        platformForm = platformForm == null ? RadioBoxView.ONE : platformForm;

        // 初始化接驳设施List
        String connectionFacilities = roadFacilitiesBusStop.getConnectionFacilities();
        if (StringUtils.isNotEmpty(connectionFacilities)) {
            connectionFacilitiesList = new ArrayList<>(Arrays.asList(connectionFacilities.split(",")));
        }

        binding.siteName.setValue(roadFacilitiesBusStop.getSiteName());
        binding.stationCoordinates.setValue(selectedPoint.getxCoordinate(), selectedPoint.getyCoordinate());
        binding.platformType.setChecked(platformType);
        binding.platformForm.setChecked(platformForm);
        binding.berths.setValue(roadFacilitiesBusStop.getBerths());
        binding.platformLength.setValue(platformLength);
        binding.stopLine.setValue(roadFacilitiesBusStop.getStopLine());
        binding.connectionFacilities.setValue(connectionFacilities);
        binding.distanceNearIntersection.setValue(distanceNearIntersection);
        binding.sidewalkWidth.setValue(sidewalkWidth);
    }

    /**
     * 获取页面绑定的值
     */
    public RoadFacilitiesBusStop getData() {
        // 泊位数整数校验
        if (binding.berths.getBigDecimalValue() != null && !CommonUtils.isIntegerValue(binding.berths.getBigDecimalValue())) {
            BamToast.showText(getContext(), "公交站点 泊位数 请输入整数！", false);
            return null;
        }
        // 将泊位数转换为Integer类型
        Integer berths = binding.berths.getBigDecimalValue() == null ? null : binding.berths.getBigDecimalValue().intValue();

        // 添加路段调查任务ID
        RoadFacilitiesInvestigationTask facilitiesInvestigationTask = roadFacilitiesTaskDTO.getFacilitiesInvestigationTask();
        if (facilitiesInvestigationTask != null && StringUtils.isNotEmpty(facilitiesInvestigationTask.getId())) {
            roadFacilitiesBusStop.setRoadInvestigationTaskId(facilitiesInvestigationTask.getId());
        }
        roadFacilitiesBusStop.setSiteName(binding.siteName.getStringValue());
        roadFacilitiesBusStop.setStationCoordinates(selectedPoint);
        roadFacilitiesBusStop.setPlatformForm(binding.platformForm.getChecked());
        roadFacilitiesBusStop.setPlatformType(binding.platformType.getChecked());
        roadFacilitiesBusStop.setBerths(berths);
        roadFacilitiesBusStop.setPlatformLength(binding.platformLength.getBigDecimalValue());
        roadFacilitiesBusStop.setStopLine(binding.stopLine.getStringValue());
        roadFacilitiesBusStop.setConnectionFacilities(binding.connectionFacilities.getStringValue());
        roadFacilitiesBusStop.setDistanceNearIntersection(binding.distanceNearIntersection.getBigDecimalValue());
        roadFacilitiesBusStop.setSidewalkWidth(binding.sidewalkWidth.getBigDecimalValue());
        return roadFacilitiesBusStop;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        SelectedPoint centerPoint = null;
        //选择了坐标
        if (resultCode == Constants.RESULT_CODE.SELECTED_POINT) {
            if (data != null) {
                Bundle presentValue = data.getExtras();
                centerPoint = (SelectedPoint) presentValue.getSerializable(Constants.EXTRA_KEY.MAP_CENTER_POINT);
            }
        }
        if (centerPoint == null) {
            return;
        }
        selectedPoint = JSONObject.toJSONString(centerPoint);
    }

    @OnClick({R.id.connectionFacilities})
    public void onViewClicked(View view) {
        //隐藏系统软键盘
        TextViewUtil.closeKeyboard(getActivity());
        RollUtils.showCheck(getContext(), connectionFacilitiesDict, connectionFacilitiesList, list -> {
            connectionFacilitiesList = (List<String>) list;
            binding.connectionFacilities.setValue(CommonUtils.listToStringCommaSplicing(list));
        });
    }
}